package week4;
import java.util.*;

public class Day1 {

    //1.小易的升级之路
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            long x = in.nextLong();
            long[] arr = new long[n];
            for(int i=0;i<n;i++) {
                arr[i]=in.nextLong();
                if(x>=arr[i]) x+=arr[i];
                else {
                    x += func(x,arr[i]);
                }
            }
            System.out.println(x);
        }
    }
    public static long func(long x,long y) {
        while(y!=0) {
            long tmp = x%y;
            x=y;
            y=tmp;
        }
        return x;
    }
    //2.礼物的最大价值

    public int maxValue (int[][] grid) {
        // write code here
        int n = grid.length;
        int m = grid[0].length;
        int[][] dp = new int[n][m];
        dp[0][0]=grid[0][0];
        for(int i=1;i<m;i++) dp[0][i]=grid[0][i]+dp[0][i-1];
        for(int i=1;i<n;i++) dp[i][0]=grid[i][0]+dp[i-1][0];
        for(int i=1;i<n;i++) {
            for(int j=1;j<m;j++) {
                dp[i][j]=grid[i][j]+Math.max(dp[i-1][j],dp[i][j-1]);
            }
        }
        return dp[n-1][m-1];
    }

    //3.对称之美
    static List<String> ret = new ArrayList<>();
    public static void main3(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();//t组数据
        for(int k=0;k<t;k++) {
            int n = in.nextInt();
            String[] str = new String[n];
            for (int i = 0; i < n; i++) str[i] = in.next();
            if (n == 1) {
            }
            dfs(str, 0, "");
            //ret里面就是所有字符串的组合
            int i = 0;
            for (; i < ret.size(); i++) {
                String tmp = ret.get(i);
                if (func(tmp)) {
                    System.out.println("Yes");
                    break;
                }
            }
            if (i == ret.size())
                System.out.println("No");
            ret.clear();
        }
    }
    public static void dfs(String[] str,int pos,String s) {
        if(s.length()==str.length) {
            ret.add(s);
            return;
        }
        for(int i=0;i<str[pos].length();i++) {
            dfs(str,pos+1,s+str[pos].charAt(i));
        }
    }
    public static boolean func(String s) {
        int left=0;
        int right=s.length()-1;
        while(left<right) {
            if(s.charAt(left)!=s.charAt(right)) return false;
            left++;
            right--;
        }
        return true;
    }
}
